home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1188 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  68 lines

  1. Path: news.umbc.edu!not-for-mail
  2. From: schlein@umbc.edu (Jonas J. Schlein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: allocating unlimited string input....HELP
  5. Date: 11 Jan 1996 21:09:44 -0500
  6. Organization: University of Maryland Baltimore County
  7. Message-ID: <4d4ft8$o7i@umbc9.umbc.edu>
  8. References: <4cvph6$s8s@pulp.ucs.ualberta.ca>
  9. NNTP-Posting-Host: f-umbc9.umbc.edu
  10. NNTP-Posting-User: schlein
  11.  
  12. Bobby Sixkiller <ryangall@gpu.srv.ualberta.ca> wrote:
  13. |> I am doing an assignment for my computing class, and seem to have run into 
  14. |> a small problem. I am supposed to be able to read an unlimited string from 
  15. |> stdin, without defining maximum size. I thought that this problem would be 
  16. |> simple....but I have been at it for a while now. Ill show you what my last 
  17. |> resort was.....
  18.  
  19. Obviously an unlimited string is impossible since computers have a finite
  20. amount of memory. However, I think I know what you mean.
  21.  
  22. <code deleted>
  23.  
  24. |> I know its ugly, but its the third version....and I tried all the other 
  25. |> possibilities that I know of that would have better style. Anyways,  they 
  26. |> all pretty much are doing the same thing...oh yeah, about char *S, thats 
  27. |> for later, Im still stuck trying to get the bulk of the function to work, 
  28. |> once its working, I will assign A to S........
  29.  
  30. Even if you did assign A to S in C variables are pass by value, so it
  31. would mean nothing. Returning the allocated string is a much nicer
  32. approach.
  33.  
  34. |> Anyways, the function reads in just fine, but for some reason when it gets 
  35. |> to 146 characters it bails out, loses char *B and starts reducing 
  36. |> count?!&*#$@   what the hell?!
  37.  
  38. You code seemed to work fine for me. The only thing I can think of is either
  39. on your machine 146 is the most amount of characters that can be on one
  40. command line, i.e. you gave '\n' as the condition to stop, or free()
  41. isn't doing a good job.
  42.  
  43. Here's a solution that uses realloc() instead of malloc()/free(). It's
  44. not the most efficient solution, but it should get you started.
  45.  
  46. char *read (void)
  47. {
  48.   int   c   = '\0';
  49.   int   len = 0;
  50.   char *str = NULL;
  51.  
  52.   do {
  53.      if ((str = realloc (str, ++len)) == NULL)
  54.         exit (1);
  55.      if (c != '\0')
  56.         str[len - 2] = (char) c;
  57.      str[len - 1] = '\0';
  58.   }  while ((c = getchar ()) != '\n');
  59.  
  60.   return (str);
  61. }
  62.  
  63. Just make sure you #include <stdio.h> and #include <stdlib.h>.
  64. -- 
  65. "If it wasn't for C, we would be using BASI, PASAL, and OBOL."
  66.  
  67. Jonas J. Schlein  (schlein@gl.umbc.edu)
  68.